// pages/mine/vip/vip.js
const $api = require("../../../api/mine").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
<<<<<<< HEAD
    userinfo:wx.getStorageSync('child_one'),
    taocan:0,
    home:[],
    homelists:[],

=======
    userinfo:wx.getStorageSync('user'),
    taocan:1
>>>>>>> 1c9ce1fdc7fcbf1a028b999eb3cd07f698a8a329
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var t = this
    t.getCameraVip()
    t.setData({
<<<<<<< HEAD
      userinfo:wx.getStorageSync('child_one'),
      classinfo:wx.getStorageSync('classinfo')
    })
    t.getMemberVipList()
  },
  choosetaocan(e){
    var t = this
    console.log(e)
    t.setData({
      taocan:e.currentTarget.dataset.index
    })
  },
    // 我的会员 套餐列表
    getCameraVip() {
      var t = this,
      data = {
        member_id:wx.getStorageSync('user').member_id,
        child_id:wx.getStorageSync('child_one').child_id,
        institution_id:wx.getStorageSync('classinfo').institution_id
      }
      $api.getCameraVip(data).then(res=>{
        console.log(res)
        t.setData({
          camera_list:res.data
        })
        t.getmoney()
      })
    },
    scroll(e){
      console.log(e)
    },
    // 用户的会员信息
  getMemberVipList() {
    var t = this,
    data = {
      member_id:wx.getStorageSync('user').member_id
    }
    $api.getMemberVipList(data).then(res=>{
      t.setData({
        vipinfo:res.data
      })
    })
  },
  addfamily(){
    var t = this
    wx.navigateTo({
      url: '/pages/mine/vip/family/index',
    })
  },
  delhomeinfo(e){
    var t = this
    var homelists = t.data.homelists
    var home = t.data.home
    home.splice(home.findIndex(index => index === homelists[e.currentTarget.dataset.index].member_id),1)
    homelists.splice(e.currentTarget.dataset.index,1)
    t.setData({
      home:home,
      homelists:homelists
    })
    t.getmoney()
  },
  getmoney(){
    var t = this
    var counts = t.data.homelists.length + 1
    var camera_list = t.data.camera_list
    for(var i in camera_list){
      var money = 0
      for(var j=0;j < counts;j++){
        if(j < camera_list[i].params.length){
          money += parseFloat(camera_list[i].params[j].money)
        }else{
          money += parseFloat(camera_list[i].params[0].money)
        }
        
      }      
      camera_list[i].money = money
    }
    t.setData({
      camera_list:camera_list
    })
  },
  // 我的会员 - 生成订单
  saveCameraVipOrder() {
    var t = this,
    data = {
      item_id:t.data.camera_list[t.data.taocan].camera_product_id,
      item_total_price:parseFloat(t.data.camera_list[t.data.taocan].money),
      child_id:t.data.userinfo.child_id,
      member_id:wx.getStorageSync('user').member_id,
      home:t.data.home.join(","),
      month_count:t.data.camera_list[t.data.taocan].month_count,
      institution_id:t.data.classinfo.institution_id,
      grade_id:t.data.classinfo.grade_id,
      class_id:t.data.classinfo.class_id,
    }
    wx.showModal({

      title: '是否确认支付',
 
      content: '金额:'+data.item_total_price + '元\r\n时长:'+data.month_count + '个月\r\n',
 
      success: function (res) {
 
        if (res.confirm) {//这里是点击了确定以后
          $api.saveCameraVipOrder(data).then(res=>{
            var data1 = {
              member_type:wx.getStorageSync('user').member_type,
              body:"开通会员",
              out_trade_no:res.data.order_no,
              attach:wx.getStorageSync('user').member_id,
              pay_type:2
            }
            $api.wxpayUnifiedOrder(data1).then(res=>{
              console.log(res)
              wx.requestPayment({
                nonceStr: res.data.noncestr,
                package: res.data.package,
                // package:`prepay_id=${res.data.prepayid}`,
                paySign: res.data.sign,
                timeStamp: res.data.timestamp,
                signType:"MD5",
                success(res){
                  console.log(res)
                  wx.showToast({
                    title: '支付成功',
                    duration: 2000,
                    icon: none,
                    mask: true,
                  })
                },
                fail (res) {
                  console.log(res)
                  wx.showToast({
                    title: '支付失败',
                    duration: 2000,
                    icon: none,
                    mask: true,
                  })
                 }
              })
            })
          })
          console.log('用户点击确定')
 
        } else {//这里是点击了取消以后
 
          console.log('用户点击取消')
 
        }
 
      }
 
    })
    console.log(data)
=======
      userinfo:wx.getStorageSync('user'),
      classinfo:wx.getStorageSync('classinfo')
    })
    t.getMemberVipList()
  },
  choosetaocan(e){
    var t = this
    console.log(e)
    t.setData({
      taocan:e.currentTarget.dataset.index
    })
  },
    // 我的会员 套餐列表
    getCameraVip() {
      var t = this,
      data = {
        member_id:wx.getStorageSync('user').member_id,
        child_id:wx.getStorageSync('child_one').child_id,
        institution_id:wx.getStorageSync('classinfo').institution_id
      }
      $api.getCameraVip(data).then(res=>{
        console.log(res)
        t.setData({
          camera_list:res.data

        })
      })
    },
    scroll(e){
      console.log(e)
    },
    // 用户的会员信息
  getMemberVipList() {
    var t = this,
    data = {
      member_id:wx.getStorageSync('user').member_id
    }
    $api.getMemberVipList(data).then(res=>{
      t.setData({
        vipinfo:res.data
      })
    })
>>>>>>> 1c9ce1fdc7fcbf1a028b999eb3cd07f698a8a329
  }
})